require(['grid', 'highcharts', 'reportCommon'], function () {
    var $grid = $("#grid"), drvId, dateType = "02";
    var goCarInfo,goLineInfo;
    function driverChange(event, treeId, treeNode) {
        if (treeNode.type === 'DRIVER') {
            drvId = treeNode.id;
            getWorkDura(drvId, dateType);
            getDrvAttnPage(drvId, dateType);
        } else {
            $.messager.alert("提示", "请选择驾驶员进行查看。");
        }
    }

    //左侧树
    commonFun.leftTreeNodeClickObj['drvAnaAttendanInfo'] = driverChange;

    //顶部日期类型
    $(".time-select a").click(function () {
        if (drvId) {
            dateType = $(this).attr('val');
            $(this).addClass("active");
            $(this).siblings().removeClass("active");
            getWorkDura(drvId, dateType);
            getDrvAttnPage(drvId, dateType);
        }
    });

    //工作时长
    function getWorkDura(drvId, dateType) {
        $.ajaxExt({
            url: "/drvAna/getWorkDura.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            var category = [], drvWorkDuraArr = [], lineAveWorkDuraArr = [];
            if (data.length > 0) {
                $(data).each(function (i, item) {
                    xAxisDateFormat(dateType, category, item.REPORT_DATE);
                    if (item.TREND_VAL1 == "驾驶员工作趋势") {
                        drvWorkDuraArr.push(parseFloat(item.TREND_VAL2));
                    } else if (item.TREND_VAL1 == "线路平均工作趋势") {
                        lineAveWorkDuraArr.push(parseFloat(item.TREND_VAL2));
                    }
                });
                workDura($.unique(category), drvWorkDuraArr, lineAveWorkDuraArr);
            } else {
                workDura("", "", "");
            }
        });
    }

    //柱状图
    function workDura(category, drvWorkDuraArr, lineAveWorkDuraArr) {
        $('#workDura').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: ''
            },
            subtitle: {
                text: ''
            },
            credits: {
                enabled: false
            },
            xAxis: {
                categories: category
            },
            yAxis: {
                min: 0,
                title: {
                    text: ''
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} 小时</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: [{
                name: '驾驶员工作时长',
                data: drvWorkDuraArr

            }, {
                name: '线路平均工作时长',
                data: lineAveWorkDuraArr

            }]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/drvAna/getDrvAttnPage.htm",
            colModel: [
                {name: "carId", label: 'carId', key: true, hidden: true},
                {name: "lineId", label: 'lineId', hidden: true},
                {name: "lineName", label: '运营线路', width: 100,formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	goLineInfo(rowObject.lineId);
	                }
	            }},
                {name: "timecardCardno", label: '考勤卡卡号', width: 100},
                {name: "drvName", label: '驾驶员', width: 100},
                {name: "licPltNo", label: '车牌号', width: 100,formatter: function(cellvalue, options, rowObject){
                	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\""+rowObject.carId+"\")' class='text-link c-blue'>"+cellvalue+"</a></b>"
                }},
                {name: "signInTime", label: '签到时间', width: 100},
                {name: "signOutTime", label: '签退时间', width: 150},
                {
                    name: "workDura", label: '工作时长', width: 100, formatter: function (cellvalue, options, rowObject) {
                    return millisecondToDate(rowObject.workDura);
                }
                }
            ],
            multiselect: false,
            width: $(window).width() - 50,
            height: $(window).height() - 285
        });
    }

    function getDrvAttnPage(drvId, dateType) {
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: {'drvId': drvId, 'dateType': dateType}
        }).trigger("reloadGrid");
    }

    //毫秒转时分秒
    function millisecondToDate(msd) {
        var time = parseFloat(msd) / 1000;
        if (null != time && "" != time) {
            if (time > 60 && time < 60 * 60) {
                time = parseInt(time / 60.0) + "分钟" + parseInt((parseFloat(time / 60.0) -
                parseInt(time / 60.0)) * 60) + "秒";
            }
            else if (time >= 60 * 60 && time < 60 * 60 * 24) {
                time = parseInt(time / 3600.0) + "小时" + parseInt((parseFloat(time / 3600.0) -
                parseInt(time / 3600.0)) * 60) + "分钟" +
                parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
                parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";
            }
            else {
                time = parseInt(time) + "秒";
            }
        }
        return time;
    }
    
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }
    
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }

    gridInit();

    //如果左侧树有选择车辆则直接查询该车辆数据
    business.execFnByLeftTreeSelectNode('DRIVER', driverChange);
});